﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Data.SqlClient;
using Telerik.Web.UI;
using KPIS.GERP.SECURITY.BLL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ComponentEvaluate : System.Web.UI.Page
    {
        private IComponentEvaluateBLL CreatComponentEvaluate = new ComponentEvaluateBLL();
        private HRDEvaluationBLL cmd = new HRDEvaluationBLL();
        private UserBLL userBLL = new UserBLL();
        private SsMng ssMng = new SsMng();
        private SsMng creatby = new SsMng();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnApproved.Visible = false;
                RadTreeList1.Visible = false;

                LoadcboRound();

                DataSet ds = userBLL.LoadUser(Int32.Parse(ssMng.UserSeq), (int?) null, (int?) null, "");
                if (ds != null)
                {
                    DataTable dt = null;

                    dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                        lblNamePos.Text = dt.Rows[0]["USER_NAME"].ToString();
                }
            }
            else if (cboRound.SelectedIndex > 0)
            {
                int? round_seq = main.ConvertNullInt(cboRound.SelectedValue);
                if (round_seq.HasValue)
                {
                    DataTable dt = CreatComponentEvaluate.LoadJobSite(creatby.EmpSeqI, round_seq);
                    RadTreeList1.DataSource = dt;
                    RadTreeList1.DataBind();
                }
            }
        }

        public void LoadcboRound()
        {
            cboRound.DataSource = CreatComponentEvaluate.LoadRound();
            cboRound.DataBind();
            cboRound.Items.Insert(0, new RadComboBoxItem("เลือกรอบการประเมิน", ""));
        }

        protected void cboRound_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRound.SelectedIndex > 0)
            {
                RadTreeList1.Visible = true;
                int? round_seq = main.ConvertNullInt(cboRound.SelectedValue);
                DataTable dt = CreatComponentEvaluate.LoadJobSite(creatby.EmpSeqI, round_seq);
                RadTreeList1.DataSource = dt;
                RadTreeList1.DataBind();
                RadTreeList1.ExpandAllItems();

                DataTable dtExec = cmd.getPosSeq(Convert.ToInt32(ssMng.EmpSeq));
                if(dtExec.Rows[0]["exec_pos_seq"] == DBNull.Value) return;
                int intExecPosSeq = Convert.ToInt32(dtExec.Rows[0]["exec_pos_seq"]);
                if (intExecPosSeq == 251)
                {
                    btnApproved.Visible = true;
                }
                else
                {
                    btnApproved.Visible = false;
                }
            }
            else
            { 
                btnApproved.Visible = false;
                RadTreeList1.Visible = false;
            }
        }

        protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
        {
            DataTable dtExec = cmd.getPosSeq(Convert.ToInt32(ssMng.EmpSeq));
            if(dtExec.Rows[0]["exec_pos_seq"] == DBNull.Value) return;

            int intExecPosSeq = Convert.ToInt32(dtExec.Rows[0]["exec_pos_seq"]);
            
            if (e.Item is TreeListDataItem)
            {
                TreeListDataItem item = e.Item as TreeListDataItem;
                HyperLink link = (HyperLink)e.Item.FindControl("hplEmpName");
                CheckBox chk = (CheckBox)e.Item.FindControl("chkIsevaluate");
                CheckBox chkApp = (CheckBox)item.FindControl("chkIsapprove");

                if (chk.Checked)
                {
                    link.NavigateUrl = string.Format("HRDEvaluationSuperVisor.aspx?ESeq={0}&RSeq={1}&YSeq={2}", item["emp_seq"].Text, item["round_seq"].Text, item["user_seq"].Text);
                }

                if (chk.Checked && intExecPosSeq == 251)
                {
                    chkApp.Enabled = true;                    
                }
            }
        }

        protected void btnApproved_Click(object sender, System.EventArgs e)
        {
            int intEmpseq = 0;
            int intRoundSeq = 0;
            int intCreateBy = 0;
            bool boolApproved = false;

            foreach (TreeListDataItem item in RadTreeList1.Items)
            {
                CheckBox chkApp = (CheckBox)item.FindControl("chkIsapprove");

                intEmpseq = Convert.ToInt32(item["emp_seq"].Text);
                intRoundSeq = Convert.ToInt32(item["round_seq"].Text);
                intCreateBy = Convert.ToInt32(ssMng.EmpSeq);
                boolApproved = chkApp.Checked;
                
                cmd.UpdateApproved(intEmpseq, intRoundSeq, intCreateBy, boolApproved);
            }   
        }
    }
}